<?php
session_start();
if(empty($_SESSION['token']['access_token'])){
	header("Location:./weibologin.php");
	exit;
}
error_reporting(0);
$msg='';

//99zhaogong网配置文件
define("APP_ROOT",dirname(dirname(__FILE__)));
defined("_D_") || define("_D_",DIRECTORY_SEPARATOR);
defined('IN_QISHI') || define('IN_QISHI',true);
require(APP_ROOT._D_."data"._D_."config.php");
require(APP_ROOT._D_."include"._D_."mysql.class.php");
header("Content-Type:text/html;charset=utf-8");
$conn = new mysql($dbhost,$dbuser,$dbpass,$dbname,'utf8');
$conn->showError=false;
$dayTime=strtotime(date("Y-m").'-1');
$sql="select id,title from qs_article where addtime > $dayTime and author !='weibo'";
$article = $conn->getSingleCol($sql,'id','title');
$randkey=array_rand($article);
if($randkey <= 0){
	var_dump($article);exit;
}
$sql='update qs_article set author=\'weibo\' where id='.$randkey;
$conn->query($sql);

//微博配置文件
define('weiboRt',dirname(__FILE__));
require_once( weiboRt.'/weibodata/weibo.config.php' );
require_once( weiboRt.'/saetv2.ex.class.php' );
require_once( weiboRt.'/weibo.ex.class.php' );

$client = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
//$annotations=array('comment'=>'testting from jken');
$text=$article[$randkey];
$purl='http://www.99zhaogong.com/news/news-show.php?id='.$randkey;

$text=$text." \r\n from:".$purl;
$message=$client->update($text,null,null,'[]');
if(!empty($message['error'])){
	$msg .=  "\n".$message['error']."\n\n";
	file_put_contents($tokenFile,'');
	$cookieFile=dirname($tokenFile.'/weibocookie.txt');
	file_put_contents($cookieFile,'');
}
$msg .=  "all Done !";







$msg=str_replace("\n",'<br />',$msg);
echo $msg;exit;